-- [tsx]: SaveManager.tsx
local ____lualib = require("lualib_bundle") -- 1
local __TS__Iterator = ____lualib.__TS__Iterator -- 1
local __TS__StringSplit = ____lualib.__TS__StringSplit -- 1
local __TS__StringSubstring = ____lualib.__TS__StringSubstring -- 1
local __TS__ParseFloat = ____lualib.__TS__ParseFloat -- 1
local __TS__Number = ____lualib.__TS__Number -- 1
local __TS__NumberIsNaN = ____lualib.__TS__NumberIsNaN -- 1
local __TS__StringAccess = ____lualib.__TS__StringAccess -- 1
local __TS__Class = ____lualib.__TS__Class -- 1
local ____exports = {} -- 1
local ____Dora = require("Dora") -- 3
local Content = ____Dora.Content -- 3
local Vec2 = ____Dora.Vec2 -- 3
____exports.actName = {"新存档", "第一幕", "银杏小镇", "终幕"} -- 6
local function savefileToText(savefile) -- 8
	local lines = {} -- 9
	lines[#lines + 1] = "act:" .. tostring(savefile.act) -- 10
	lines[#lines + 1] = "playTime:" .. tostring(savefile.playTime) -- 11
	lines[#lines + 1] = (("player_pos:" .. tostring(savefile.player_pos.x)) .. ",") .. tostring(savefile.player_pos.y) -- 12
	local taskParts = {} -- 13
	for ____, task in __TS__Iterator(savefile.tasks) do -- 14
		local escapedTask = task -- 15
		local index = -1 -- 16
		while true do -- 16
			index = escapedTask.indexOf("\"", index + 1) -- 17
			if not (index ~= -1) then -- 17
				break -- 17
			end -- 17
			escapedTask = (tostring(escapedTask.substring(0, index)) .. "\\\"") .. tostring(escapedTask.substring(index + 1)) -- 18
			index = index + 1 -- 19
		end -- 19
		if escapedTask.includes(",") or escapedTask.includes("\"") then -- 19
			taskParts[#taskParts + 1] = ("\"" .. tostring(escapedTask)) .. "\"" -- 22
		else -- 22
			taskParts[#taskParts + 1] = escapedTask -- 24
		end -- 24
	end -- 24
	lines[#lines + 1] = "tasks:" .. table.concat(taskParts, ",") -- 27
	local packageParts = {} -- 28
	for ____, item in __TS__Iterator(savefile.package) do -- 29
		packageParts[#packageParts + 1] = tostring(item) -- 30
	end -- 30
	lines[#lines + 1] = "package:" .. table.concat(packageParts, ",") -- 32
	local progressParts = {} -- 33
	for ____, prog in __TS__Iterator(savefile.progress) do -- 34
		progressParts[#progressParts + 1] = tostring(prog) -- 35
	end -- 35
	lines[#lines + 1] = "progress:" .. table.concat(progressParts, ",") -- 37
	return table.concat(lines, "\n") -- 38
end -- 8
local function textToSavefile(text) -- 41
	local lines = __TS__StringSplit(text, "\n") -- 42
	local result = {} -- 43
	for ____, line in ipairs(lines) do -- 44
		do -- 44
			local colonIndex = (string.find(line, ":", nil, true) or 0) - 1 -- 45
			if colonIndex == -1 then -- 45
				goto __continue13 -- 46
			end -- 46
			local key = __TS__StringSubstring(line, 0, colonIndex) -- 47
			local value = __TS__StringSubstring(line, colonIndex + 1) -- 48
			repeat -- 48
				local ____switch15 = key -- 48
				local coords, tasks, current, inQuotes, escapeNext, numStrs, nums -- 48
				local ____cond15 = ____switch15 == "act" or ____switch15 == "playTime" -- 48
				if ____cond15 then -- 48
					result[key] = __TS__ParseFloat(value) -- 52
					break -- 53
				end -- 53
				____cond15 = ____cond15 or ____switch15 == "player_pos" -- 53
				if ____cond15 then -- 53
					coords = __TS__StringSplit(value, ",") -- 55
					if #coords >= 2 then -- 55
						local x = __TS__ParseFloat(coords[1]) -- 57
						local y = __TS__ParseFloat(coords[2]) -- 58
						if not __TS__NumberIsNaN(__TS__Number(x)) and not __TS__NumberIsNaN(__TS__Number(y)) then -- 58
							result[key] = Vec2(x, y) -- 60
						end -- 60
					end -- 60
					break -- 63
				end -- 63
				____cond15 = ____cond15 or ____switch15 == "tasks" -- 63
				if ____cond15 then -- 63
					tasks = {} -- 65
					current = "" -- 66
					inQuotes = false -- 67
					escapeNext = false -- 68
					do -- 68
						local i = 0 -- 69
						while i < #value do -- 69
							local char = __TS__StringAccess(value, i) -- 70
							if escapeNext then -- 70
								current = current .. char -- 72
								escapeNext = false -- 73
							elseif char == "\\" then -- 73
								escapeNext = true -- 75
							elseif char == "\"" then -- 75
								inQuotes = not inQuotes -- 77
							elseif char == "," and not inQuotes then -- 77
								tasks[#tasks + 1] = current -- 79
								current = "" -- 80
							else -- 80
								current = current .. char -- 82
							end -- 82
							i = i + 1 -- 69
						end -- 69
					end -- 69
					if current ~= "" then -- 69
						tasks[#tasks + 1] = current -- 86
					end -- 86
					result[key] = tasks -- 88
					break -- 89
				end -- 89
				____cond15 = ____cond15 or (____switch15 == "package" or ____switch15 == "progress") -- 89
				if ____cond15 then -- 89
					numStrs = __TS__StringSplit(value, ",") -- 92
					nums = {} -- 93
					for ____, numStr in ipairs(numStrs) do -- 94
						local num = __TS__ParseFloat(numStr) -- 95
						if not __TS__NumberIsNaN(__TS__Number(num)) then -- 95
							nums[#nums + 1] = num -- 97
						end -- 97
					end -- 97
					result[key] = nums -- 100
					break -- 101
				end -- 101
			until true -- 101
		end -- 101
		::__continue13:: -- 101
	end -- 101
	local finalResult = { -- 104
		act = result.act or 1, -- 105
		playTime = result.playTime or 0, -- 106
		player_pos = result.player_pos or Vec2(0, 0), -- 107
		tasks = result.tasks or ({}), -- 108
		package = result.package or ({}), -- 109
		progress = result.progress or ({}) -- 110
	} -- 110
	return finalResult -- 112
end -- 41
____exports.SaveManager = __TS__Class() -- 116
local SaveManager = ____exports.SaveManager -- 116
SaveManager.name = "SaveManager" -- 116
function SaveManager.prototype.____constructor(self) -- 119
	self.saveDir = "Save" -- 117
	self:ensureSaveDir() -- 120
end -- 119
function SaveManager.prototype.ensureSaveDir(self) -- 124
	if not Content:isdir(self.saveDir) then -- 124
		Content:mkdir(self.saveDir) -- 126
	end -- 126
end -- 124
function SaveManager.prototype.getSavePath(self, slot) -- 131
	return (self.saveDir .. "/save") .. tostring(slot) -- 132
end -- 131
function SaveManager.prototype.createDefaultSave(self) -- 134
	return { -- 135
		act = 0, -- 136
		playTime = 0, -- 137
		player_pos = Vec2(0, 0), -- 138
		tasks = {}, -- 139
		package = {}, -- 140
		progress = {} -- 141
	} -- 141
end -- 134
function SaveManager.prototype.getAllSaves(self, maxSlots) -- 145
	local saves = {} -- 146
	self:ensureSaveDir() -- 147
	do -- 147
		local slot = 0 -- 149
		while slot < maxSlots do -- 149
			local savePath = self:getSavePath(slot) -- 150
			if not Content:exist(savePath) then -- 150
				local defaultSave = self:createDefaultSave() -- 155
				local content = savefileToText(defaultSave) -- 156
				Content:save(savePath, content) -- 157
				saves[#saves + 1] = defaultSave -- 158
			else -- 158
				do -- 158
					local function ____catch(____error) -- 158
						print( -- 167
							("读取存档 " .. tostring(slot)) .. " 失败:", -- 167
							____error -- 167
						) -- 167
						local defaultSave = self:createDefaultSave() -- 168
						saves[#saves + 1] = defaultSave -- 169
						do -- 169
							local function ____catch(saveError) -- 169
								print( -- 176
									("修复存档 " .. tostring(slot)) .. " 失败:", -- 176
									saveError -- 176
								) -- 176
							end -- 176
							local ____try, ____hasReturned = pcall(function() -- 176
								local content = savefileToText(defaultSave) -- 173
								Content:save(savePath, content) -- 174
							end) -- 174
							if not ____try then -- 174
								____catch(____hasReturned) -- 174
							end -- 174
						end -- 174
					end -- 174
					local ____try, ____hasReturned = pcall(function() -- 174
						local content = Content:load(savePath) -- 162
						local save = textToSavefile(content) -- 163
						saves[#saves + 1] = save -- 164
					end) -- 164
					if not ____try then -- 164
						____catch(____hasReturned) -- 164
					end -- 164
				end -- 164
			end -- 164
			slot = slot + 1 -- 149
		end -- 149
	end -- 149
	return saves -- 181
end -- 145
function SaveManager.prototype.saveGame(self, slot, savefile) -- 185
	local content = savefileToText(savefile) -- 186
	local savePath = self:getSavePath(slot) -- 187
	return Content:save(savePath, content) -- 188
end -- 185
function SaveManager.prototype.deleteSave(self, slot) -- 191
	local content = savefileToText(self:createDefaultSave()) -- 192
	local savePath = self:getSavePath(slot) -- 193
	return Content:save(savePath, content) -- 194
end -- 191
return ____exports -- 191